package org.dtrd.modules.evaluation.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.dtrd.modules.evaluation.entity.bean.AngiocardiopathyInfo;
import org.dtrd.modules.evaluation.entity.po.DtrdEntEvaluationRdAngiocardiopathy;
import org.dtrd.modules.evaluation.entity.request.AngiocardiopathyRequest;
import org.dtrd.modules.evaluation.mapper.DtrdEntEvaluationRdAngiocardiopathyMapper;
import org.dtrd.modules.evaluation.service.IDtrdEntEvaluationRdAngiocardiopathyService;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.StringJoiner;

/**
 * <p>
 * 10年心血管疾病发病评估 服务实现类
 * </p>
 *
 * @author QR
 * @since 2021-09-26
 */
@Service
@DS("multi-datasource1")
public class DtrdEntEvaluationRdAngiocardiopathyServiceImpl extends ServiceImpl<DtrdEntEvaluationRdAngiocardiopathyMapper, DtrdEntEvaluationRdAngiocardiopathy> implements IDtrdEntEvaluationRdAngiocardiopathyService {

    @Override
    public AngiocardiopathyInfo saveAngiocardiopathyInfo(AngiocardiopathyRequest dto) {
        List<Integer> input = dto.getInput();
        // 性别
        int gender = input.get(0);
        // 年龄
        int age;
        Integer ageOption = input.get(1);
        // 设置年龄
        switch (ageOption) {
            case 0:
                age = 37;
                break;
            case 1:
                age = 42;
                break;
            case 2:
                age = 47;
                break;
            case 3:
                age = 52;
                break;
            case 4:
                age = 57;
                break;
            case 5:
                age = 60;
                break;
            default:
                age = 30;
                break;
        }
        // 收缩压
        int pressureSystolic = input.get(2);
        // bmi
        int bmi = input.get(3);
        // tc (总胆固醇)
        int tc = input.get(4);
        // 吸烟状况
        int isSmoke = input.get(5);
        // 糖尿病状况
        int isDiabetes = input.get(6);
        int score = 0;
        // 年龄（岁）计算分数
        score += age < 35 ? 0 : ((age - 35) / 5);
        // 收缩压（mmHg）计算分数
        if (1 == gender) {
            switch (pressureSystolic) {
                // <120
                case 0:
                    score += -2;
                    break;
                // 120 ~ 129
                case 1:
                    break;
                // 130 ~ 139
                case 2:
                    score += 1;
                    break;
                // 140 ~ 159
                case 3:
                    score += 2;
                    break;
                // 160 ~ 179
                case 4:
                    score += 5;
                    break;
                // >= 180
                default:
                    score += 8;
                    break;
            }
        } else if (2 == gender) {
            switch (pressureSystolic) {
                // <120
                case 0:
                    score += -2;
                    break;
                // 120 ~ 129
                case 1:
                    break;
                // 130 ~ 139
                case 2:
                    score += 1;
                    break;
                // 140 ~ 159
                case 3:
                    score += 2;
                    break;
                // 160 ~ 179
                case 4:
                    score += 3;
                    break;
                // >= 180
                default:
                    score += 4;
                    break;
            }
        }
        // 体质指数（kg/m2）计算分数
        switch (bmi) {
            case 0:
                break;
            case 1:
                score += 1;
                break;
            default:
                score += 2;
                break;
        }
        // 总胆固醇（mmol/L）计算分数
        if (tc >= 1) {
            score += 1;
        }
        // 吸烟 计算分数
        if (1 == gender) {
            // 男性
            if (1 <= isSmoke) {
                score += 2;
            }
        } else if (2 == gender) {
            // 女性
            if (1 <= isSmoke) {
                score += 1;
            }
        }
        // 糖尿病
        if (1 == gender) {
            // 男性
            if (1 <= isDiabetes) {
                score += 1;
            }
        } else if (2 == gender) {
            // 女性
            if (1 <= isDiabetes) {
                score += 2;
            }
        }
        // 绝对危险（%）
        float caseRate = 0;
        if (1 == gender) {
            // 男性
            if (score <= -1) {
                caseRate = 0.3f;
            } else if (0 == score) {
                caseRate = 0.5f;
            } else if (1 == score) {
                caseRate = 0.6f;
            } else if (2 == score) {
                caseRate = 0.8f;
            } else if (3 == score) {
                caseRate = 1.1f;
            } else if (4 == score) {
                caseRate = 1.5f;
            } else if (5 == score) {
                caseRate = 2.1f;
            } else if (6 == score) {
                caseRate = 2.9f;
            } else if (7 == score) {
                caseRate = 3.9f;
            } else if (8 == score) {
                caseRate = 5.4f;
            } else if (9 == score) {
                caseRate = 7.3f;
            } else if (10 == score) {
                caseRate = 9.7f;
            } else if (11 == score) {
                caseRate = 12.8f;
            } else if (12 == score) {
                caseRate = 16.8f;
            } else if (13 == score) {
                caseRate = 21.7f;
            } else if (14 == score) {
                caseRate = 27.7f;
            } else if (15 == score) {
                caseRate = 35.3f;
            } else if (16 == score) {
                caseRate = 44.3f;
            } else if (score >= 17) {
                caseRate = 52.6f;
            }
        } else if (2 == gender) {
            // 女性
            if (-2 == score) {
                caseRate = 0.1f;
            } else if (-1 == score) {
                caseRate = 0.2f;
            } else if (0 == score) {
                caseRate = 0.2f;
            } else if (1 == score) {
                caseRate = 0.2f;
            } else if (2 == score) {
                caseRate = 0.3f;
            } else if (3 == score) {
                caseRate = 0.5f;
            } else if (4 == score) {
                caseRate = 1.5f;
            } else if (5 == score) {
                caseRate = 2.1f;
            } else if (6 == score) {
                caseRate = 2.9f;
            } else if (7 == score) {
                caseRate = 3.9f;
            } else if (8 == score) {
                caseRate = 5.4f;
            } else if (9 == score) {
                caseRate = 7.3f;
            } else if (10 == score) {
                caseRate = 9.7f;
            } else if (11 == score) {
                caseRate = 12.8f;
            } else if (12 == score) {
                caseRate = 16.8f;
            } else if (score >= 13) {
                caseRate = 21.7f;
            }
        }

        //计算同龄人的平均危险与最低危险值与倍数//年龄段35-39，40-44，45-49，50-54，55-59
        float[] maleAveRate = {1f,1.4f,1.9f,2.6f,3.6f};
        float[] maleMinRate = {0.3f,0.4f,0.5f,0.7f,1f};
        float[] femaleAveRate = {0.3f,0.4f,0.6f,0.9f,1.4f};
        float[] femaleMinRate = {0.1f,0.1f,0.2f,0.3f,0.5f};
        float aveRate,minRate;
        //匹配年龄段
        int i=0;
        if(age<45 && age>=40) i=1;
        else if(age<50 && age>=45) i=2;
        else if(age<55 && age>=50) i=3;
        else  i=4;
        if(1 == gender)//男
        {
            aveRate = maleAveRate[i];
            minRate = maleMinRate[i];
        }
        else
        {
            aveRate = femaleAveRate[i];
            minRate = femaleMinRate[i];
        }
        String output = "您的10年缺血性心血管病绝对危险值为" + caseRate + "，同龄人的平均危险值为"+aveRate+"，最低危险值为"+minRate+"。";
        // 有用户id就保存
        DtrdEntEvaluationRdAngiocardiopathy angiocardiopathy = new DtrdEntEvaluationRdAngiocardiopathy();
        angiocardiopathy.setCreateTime(new Date());
        angiocardiopathy.setPressureSystolic(pressureSystolic);
        angiocardiopathy.setScore(score);
        angiocardiopathy.setCaseRate(caseRate);
        angiocardiopathy.setOutput(output);
        // 保存输入的内容
        if (dto.getInput() != null) {
            StringJoiner joiner = new StringJoiner(",");
            dto.getInput().forEach(s -> joiner.add(s+""));
            angiocardiopathy.setInput(joiner.toString());
        }
        // 有 用户id 才存入数据库
        if (dto.getPatientId() != null) {
            angiocardiopathy.setPatientId(dto.getPatientId());
            // 按照源项目设置
            angiocardiopathy.setUserType(1);
            baseMapper.insert(angiocardiopathy);
        }
        return new AngiocardiopathyInfo().parseFromPo(angiocardiopathy);
    }

    @Override
    public AngiocardiopathyInfo getAngiocardiopathyInfoLatest(Integer patientId) {
        LambdaQueryWrapper<DtrdEntEvaluationRdAngiocardiopathy> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper
                .eq(DtrdEntEvaluationRdAngiocardiopathy::getPatientId, patientId)
                .eq(DtrdEntEvaluationRdAngiocardiopathy::getIsDel, 0)
                .orderByDesc(DtrdEntEvaluationRdAngiocardiopathy::getCreateTime)
                .last("limit 1");
        DtrdEntEvaluationRdAngiocardiopathy angiocardiopathy = baseMapper.selectOne(lambdaQueryWrapper);
        AngiocardiopathyInfo angiocardiopathyInfo = new AngiocardiopathyInfo();
        if (angiocardiopathy != null) {
            angiocardiopathyInfo = angiocardiopathyInfo.parseFromPo(angiocardiopathy);
        }
        return angiocardiopathyInfo;
    }
}
